package com.e3roid.examples;

import android.view.MotionEvent;

import com.e3roid.E3Activity;
import com.e3roid.E3Engine;
import com.e3roid.E3Scene;
import com.e3roid.drawable.Shape;
import com.e3roid.drawable.Sprite;
import com.e3roid.drawable.texture.Texture;
import com.e3roid.drawable.texture.AssetTexture;

public class SpriteDragExample extends E3Activity {

	private final static int WIDTH  = 320;
	private final static int HEIGHT = 480;
	
	private Sprite  robot;
	private Texture robotTexture;

	@Override
	public E3Engine onLoadEngine() {
		E3Engine engine = new E3Engine(this, WIDTH, HEIGHT);
		engine.requestFullScreen();
		engine.requestPortrait();
		return engine;
	}

	@Override
	public E3Scene onLoadScene() {
		E3Scene scene = new E3Scene();
		
		int centerX = (getWidth()  - robotTexture.getWidth())  / 2;
		int centerY = (getHeight() - robotTexture.getHeight()) / 2;
		
		robot = new Sprite(robotTexture, centerX, centerY) {
			@Override
			public boolean onTouchEvent(E3Scene scene, Shape shape,
					MotionEvent motionEvent, int localX, int localY) {
				if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
					move(getTouchEventX(scene, motionEvent) - (getWidth()/2), 
						 getTouchEventY(scene, motionEvent) - (getHeight()/2));
				}
				return false;
			}
			
		};
		scene.addEventListener(robot);
		scene.getTopLayer().add(robot);
		return scene;
	}

	@Override
	public void onLoadResources() {
		robotTexture = new AssetTexture("robot.png", this);
	}
	
}